package SmartFleet.Car;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class SmartFleetCar extends MapActivity {

	private MapView mapView;
	private MapController mc;

	private TextView batterytext;
	private TextView heightext;

	private FlyingCar myCar;

	// Need handler for callbacks to the UI thread
	final Handler mHandler = new Handler();
	// Create runnable for posting
	final Runnable mUpdateResults = new Runnable() {
		public void run() {
			updateUI();
		}
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);

		this.mapView = (MapView) findViewById(R.id.mapview);
		this.mc = mapView.getController();

		this.batterytext = (TextView) findViewById(R.id.battery);
		this.heightext = (TextView) findViewById(R.id.height);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		GeoPoint IST = new GeoPoint(38736830, -9138181);
		GeoPoint TP = new GeoPoint(38736320, -9301917);
		GeoPoint AirPort = new GeoPoint(38765775, -9133007);

		// mapView.setBuiltInZoomControls(true);
		this.mapView.setSatellite(false);
		this.mc.setZoom(16);
		RouteOverlay ro = new RouteOverlay(IST, IST, 255);
		this.mapView.getOverlays().add(ro);

		this.myCar = new FlyingCar(this.mc, ro, this.mHandler,
				this.mUpdateResults);

		this.myCar.setLocation(IST);
		this.mc.setCenter(IST);
		this.mc.animateTo(IST);

		HeartBeatService.setMainActivity(this);
		final Intent heatBeatService = new Intent(this, HeartBeatService.class);
		startService(heatBeatService);

		RecieveFlightService.setMainActivity(this);
		final Intent recieveFlightService = new Intent(this, RecieveFlightService.class);
		startService(recieveFlightService);
		
		CarInStationService.setMainActivity(this);
		final Intent carInStationService = new Intent(this, CarInStationService.class);
		startService(carInStationService);
		
	}

	// TODO: RUIQ IDEIAS??
	@Override
	protected boolean isRouteDisplayed() {

		return false;
	}

	public void setDestinationTagus(View V) {
		this.myCar.setDestination(new GeoPoint(38736320, -9301917));
	}

	public void setDestinationIST(View V) {
		this.myCar.setDestination(new GeoPoint(38736830, -9138181));
	}

	public void setDestinationAirPort(View V) {
		this.myCar.setDestination(new GeoPoint(38765775, -9133007));
	}

	public void updateUI() {
		// TODO: RUIQ este cast (int) e necessario?
		this.batterytext.setText((int) this.myCar.getPercentageBattery() + "%");
		this.heightext.setText((int) this.myCar.getHeight() + "m");
	}

	public MapView getMapView() {
		return mapView;
	}

	public void setMapView(MapView mapView) {
		this.mapView = mapView;
	}

	public MapController getMc() {
		return mc;
	}

	public void setMc(MapController mc) {
		this.mc = mc;
	}

	public TextView getBatterytext() {
		return batterytext;
	}

	public void setBatterytext(TextView batterytext) {
		this.batterytext = batterytext;
	}

	public TextView getHeightext() {
		return heightext;
	}

	public void setHeightext(TextView heightext) {
		this.heightext = heightext;
	}

	public FlyingCar getMyCar() {
		return myCar;
	}

	public void setMyCar(FlyingCar myCar) {
		this.myCar = myCar;
	}

	public Handler getmHandler() {
		return mHandler;
	}

	public Runnable getmUpdateResults() {
		return mUpdateResults;
	}
}
